home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / ODFDev / ODF / OS / FWGraphx / Sources / FWGDIObj.cpp < prev    next >
Encoding:
Text File  |  1995-11-08  |  13.5 KB  |  485 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWGDIObj.cpp
  4. //    Release Version:    $ 1.0d11 $
  5. //
  6. //    Copyright:    © 1993, 1995 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWOS.hpp"
  11.  
  12. #ifdef FW_BUILD_WIN
  13.  
  14. #ifndef FWGDIOBJ_H
  15. #include "FWGDIObj.h"
  16. #endif
  17.  
  18. #ifndef FWGRGLOB_H
  19. #include "FWGrGlob.h"
  20. #endif
  21.  
  22. #ifndef FWBWPAT_H
  23. #include "FWBWPat.h"
  24. #endif
  25.  
  26. // ----- Foundation Includes -----
  27.  
  28. #ifndef FWCOMMON_H
  29. #include <FWCommon.h>
  30. #endif
  31.  
  32. #ifndef FWEXCDEF_H
  33. #include <FWExcDef.h>
  34. #endif
  35.  
  36. #ifndef FWMEMORY_H
  37. #include <FWMemory.h>
  38. #endif
  39.  
  40. //========================================================================================
  41. //    class FW_CPrivGDIObject
  42. //========================================================================================
  43.  
  44. //----------------------------------------------------------------------------------------
  45. //    FW_CPrivGDIObject::FW_CPrivGDIObject
  46. //----------------------------------------------------------------------------------------
  47.  
  48. FW_CPrivGDIObject::FW_CPrivGDIObject() :
  49.     fGDIObject(),
  50.     fStockID(0xFFFF),
  51.     fStockObject(FALSE),
  52.     fDC(NULL),
  53.     fPreviousObject(NULL),
  54.     fChanged(FALSE)
  55. {
  56.     FW_END_CONSTRUCTOR
  57. }
  58.  
  59. //----------------------------------------------------------------------------------------
  60. //    FW_CPrivGDIObject::~FW_CPrivGDIObject
  61. //----------------------------------------------------------------------------------------
  62.  
  63. FW_CPrivGDIObject::~FW_CPrivGDIObject()
  64. {
  65.     FW_START_DESTRUCTOR
  66.  
  67.     FW_ASSERT(fDC == NULL);
  68.     FW_ASSERT(fPreviousObject == NULL);
  69.     DeleteGDIObject(fGDIObject);
  70. }
  71.  
  72. //----------------------------------------------------------------------------------------
  73. //    FW_CPrivGDIObject::SetStockID
  74. //----------------------------------------------------------------------------------------
  75.  
  76. void FW_CPrivGDIObject::SetStockID(int stockID)
  77. {
  78.     if (stockID != fStockID || !fStockObject || fGDIObject == NULL)
  79.     {
  80.         fStockID = stockID;
  81.         fStockObject = TRUE;
  82.         fChanged = TRUE;
  83.     }
  84. }
  85.  
  86. //----------------------------------------------------------------------------------------
  87. //    FW_CPrivGDIObject::UnselectObject
  88. //----------------------------------------------------------------------------------------
  89.  
  90. void FW_CPrivGDIObject::UnselectObject(HDC hdc)
  91. {
  92.     if (fPreviousObject != NULL)
  93.     {
  94.         FW_ASSERT(fDC == hdc);
  95.         FW_ASSERT(fGDIObject != NULL);    // We can't have fPreviousObject != NULL and fGDIObject == NULL
  96.     
  97.         ::SelectObject(fDC, fPreviousObject);
  98.         fPreviousObject = NULL;
  99.         fDC = NULL;
  100.     }
  101. }
  102.  
  103. //----------------------------------------------------------------------------------------
  104. //    FW_CPrivGDIObject::DeleteGDIObject
  105. //----------------------------------------------------------------------------------------
  106.  
  107. void FW_CPrivGDIObject::DeleteGDIObject(FW_SGDIObject& GDIObject)
  108. {
  109.     if (GDIObject.fObject != NULL && !GDIObject.fIsStock)
  110.         ::DeleteObject(GDIObject.fObject);
  111.     GDIObject.fObject = NULL;
  112.     GDIObject.fIsStock = FALSE;
  113. }
  114.  
  115. //----------------------------------------------------------------------------------------
  116. //    FW_CPrivGDIObject::MakeObject
  117. //----------------------------------------------------------------------------------------
  118.  
  119. void FW_CPrivGDIObject::MakeGDIObject(FW_SGDIObject& GDIObject)
  120. {
  121.     GDIObject.fObject = (GDIObject.fIsStock = fStockObject) == TRUE ? ::GetStockObject(fStockID) : CreateObject();
  122. }
  123.  
  124. //----------------------------------------------------------------------------------------
  125. //    FW_CPrivGDIObject::SelectObject
  126. //----------------------------------------------------------------------------------------
  127. //    Use to select an object into a DC
  128.  
  129. void FW_CPrivGDIObject::SelectObject(HDC hdc)
  130. {
  131.     if (fGDIObject == NULL)
  132.     {
  133.         FW_ASSERT(fDC == NULL);
  134.         FW_ASSERT(fPreviousObject == NULL);
  135.         MakeGDIObject(fGDIObject);
  136.         fPreviousObject = ::SelectObject(hdc, fGDIObject);
  137.         FW_ASSERT(fPreviousObject != NULL);
  138.         fDC = hdc;
  139.         fChanged = FALSE;
  140.     }
  141.     else if (fChanged)
  142.     {
  143.         if (fPreviousObject == NULL)
  144.         {
  145.             FW_ASSERT(fDC == NULL);
  146.  
  147.             // ----- We know it is not selected otherwise fPreviousObject would be != NULL -----
  148.             DeleteGDIObject(fGDIObject);
  149.             
  150.             MakeGDIObject(fGDIObject);
  151.             
  152.             fPreviousObject = ::SelectObject(hdc, fGDIObject);
  153.             FW_ASSERT(fPreviousObject != NULL);
  154.             fDC = hdc;
  155.         }
  156.         else
  157.         {
  158.             FW_ASSERT(fDC == hdc);
  159.             
  160.             // ----- We can't delete it before selecting something else -----
  161.             HGDIOBJ hgdiObj = fGDIObject.fObject;
  162.             BOOL bIsStock = fGDIObject.fIsStock;
  163.                         
  164.             MakeGDIObject(fGDIObject);
  165.             fPreviousObject = ::SelectObject(fDC, fGDIObject);
  166.  
  167.             // ----- Now we can delete it -----
  168.             if (!bIsStock)
  169.                 ::DeleteObject(hgdiObj);
  170.         }
  171.         
  172.         fChanged = FALSE;
  173.     }
  174.     else if (fPreviousObject == NULL)    // Never been selected
  175.     {
  176.         FW_ASSERT(fDC == NULL);
  177.         fPreviousObject = ::SelectObject(hdc, fGDIObject);
  178.         FW_ASSERT(fPreviousObject != NULL);
  179.         fDC = hdc;
  180.     }
  181. //    else
  182. //    {
  183. //        FW_ASSERT(FALSE);
  184. //    }
  185. }
  186.  
  187. //----------------------------------------------------------------------------------------
  188. //    FW_CPrivGDIObject::GetObject
  189. //----------------------------------------------------------------------------------------
  190. //    Returns an object without selecting it into a DC. Mainly used for brushes
  191.  
  192. HGDIOBJ FW_CPrivGDIObject::GetObject(HDC hdc)
  193. {
  194.     FW_ASSERT(hdc != NULL);
  195.     
  196.     if (fGDIObject == NULL)
  197.     {
  198.         FW_ASSERT(fPreviousObject == NULL);
  199.         FW_ASSERT(fDC == NULL);
  200.         MakeGDIObject(fGDIObject);
  201.         fChanged = FALSE;
  202.     }
  203.     else if (fChanged)
  204.     {
  205.         if (fPreviousObject != NULL)
  206.         {
  207.             FW_ASSERT(fDC == hdc);
  208.             FW_ASSERT(fGDIObject != NULL);
  209.             ::SelectObject(fDC, fPreviousObject);
  210.             fPreviousObject = NULL;
  211.             fDC = NULL;
  212.         }
  213.         
  214.         DeleteGDIObject(fGDIObject);
  215.         MakeGDIObject(fGDIObject);
  216.         
  217.         fChanged = FALSE;
  218.     }
  219.     
  220.     return fGDIObject.fObject;
  221. }
  222.  
  223. //========================================================================================
  224. //    class FW_CPrivGDIPen
  225. //========================================================================================
  226.  
  227. //----------------------------------------------------------------------------------------
  228. //    FW_CPrivGDIPen::FW_CPrivGDIPen
  229. //----------------------------------------------------------------------------------------
  230.  
  231. FW_CPrivGDIPen::FW_CPrivGDIPen() :
  232.     FW_CPrivGDIObject()
  233. {
  234. }
  235.  
  236. //----------------------------------------------------------------------------------------
  237. //    FW_CPrivGDIPen::~FW_CPrivGDIPen
  238. //----------------------------------------------------------------------------------------
  239.  
  240. FW_CPrivGDIPen::~FW_CPrivGDIPen()
  241. {
  242. }
  243.  
  244. //----------------------------------------------------------------------------------------
  245. //    FW_CPrivGDIPen::SetPenSize
  246. //----------------------------------------------------------------------------------------
  247.  
  248. void FW_CPrivGDIPen::SetPenSize(int width)
  249. {
  250.     if (fStockObject || fGDIObject == NULL || width != fWidth)
  251.     {
  252.         fStockObject = FALSE;
  253.         fWidth = width;
  254.         fChanged = TRUE;
  255.     }
  256. }
  257.  
  258. //----------------------------------------------------------------------------------------
  259. //    FW_CPrivGDIPen::SetPenStyle
  260. //----------------------------------------------------------------------------------------
  261.  
  262. void FW_CPrivGDIPen::SetPenStyle(FW_EStyleDash dashStyle)
  263. {
  264.     if (fStockObject || fGDIObject == NULL || dashStyle != fDashStyle)
  265.     {
  266.         fStockObject = FALSE;
  267.         fDashStyle = dashStyle;
  268.         fChanged = TRUE;
  269.     }
  270. }
  271.  
  272. //----------------------------------------------------------------------------------------
  273. //    FW_CPrivGDIPen::SetPenColor
  274. //----------------------------------------------------------------------------------------
  275.  
  276. void FW_CPrivGDIPen::SetPenColor(COLORREF color)
  277. {
  278.     if (fStockObject || fGDIObject == NULL || color != fColor)
  279.     {
  280.         fStockObject = FALSE;
  281.         fColor = color;
  282.         fChanged = TRUE;
  283.     }
  284. }
  285.  
  286. //----------------------------------------------------------------------------------------
  287. //    FW_CPrivGDIPen::CreateObject
  288. //----------------------------------------------------------------------------------------
  289.  
  290. HGDIOBJ FW_CPrivGDIPen::CreateObject()
  291. {
  292.     int nStyle = PS_INSIDEFRAME;
  293.     if (fDashStyle != FW_kSolidLine && fWidth <= 1)
  294.     {
  295.         switch (fDashStyle & ~FW_kOpaque)
  296.         {
  297.         default:
  298.             FW_ASSERT(FALSE);        // Invalid dash style
  299.                                     // fall-through
  300.         case FW_kDash:
  301.             nStyle = PS_DASH;
  302.             break;
  303.  
  304.         case FW_kDot:
  305.             nStyle = PS_DOT;
  306.             break;
  307.     
  308.         case FW_kDashDot:
  309.             nStyle = PS_DASHDOT;
  310.             break;
  311.     
  312.         case FW_kDashDotDot:
  313.             nStyle = PS_DASHDOTDOT;
  314.             break;
  315.         }
  316.     }
  317.  
  318.     return ::CreatePen(nStyle, fWidth, fColor);
  319. }
  320.  
  321. //========================================================================================
  322. //    class FW_CPrivGDIBrush
  323. //========================================================================================
  324.  
  325. //----------------------------------------------------------------------------------------
  326. //    FW_CPrivGDIBrush::FW_CPrivGDIBrush
  327. //----------------------------------------------------------------------------------------
  328.  
  329. FW_CPrivGDIBrush::FW_CPrivGDIBrush() :
  330.     FW_CPrivGDIObject(),
  331.     fPattern(FW_kBlackPat)
  332. {
  333. }
  334.  
  335. //----------------------------------------------------------------------------------------
  336. //    FW_CPrivGDIBrush::~FW_CPrivGDIBrush
  337. //----------------------------------------------------------------------------------------
  338.  
  339. FW_CPrivGDIBrush::~FW_CPrivGDIBrush()
  340. {
  341. }
  342.  
  343. //----------------------------------------------------------------------------------------
  344. //    FW_CPrivGDIBrush::SetBrushPattern
  345. //----------------------------------------------------------------------------------------
  346.  
  347. void FW_CPrivGDIBrush::SetBrushPattern(const FW_PPattern& pattern)
  348. {
  349.     if (fSolidColorBrush || fStockObject || fGDIObject == NULL || !pattern.IsSameRepAs(fPattern))
  350.     {
  351.         fStockObject = FALSE;
  352.         fSolidColorBrush = FALSE;
  353.         fPattern = pattern;
  354.         fChanged = TRUE;
  355.     }
  356. }
  357.  
  358. //----------------------------------------------------------------------------------------
  359. //    FW_CPrivGDIBrush::SetBrushColor
  360. //----------------------------------------------------------------------------------------
  361.  
  362. void FW_CPrivGDIBrush::SetBrushColor(COLORREF color)
  363. {
  364.     if (color != fColor || fStockObject || fGDIObject == NULL || !fSolidColorBrush)
  365.     {
  366.         fStockObject = FALSE;
  367.         fSolidColorBrush = TRUE;
  368.         fColor = color;
  369.         fChanged = TRUE;
  370.     }
  371. }
  372.  
  373. //----------------------------------------------------------------------------------------
  374. //    FW_CPrivGDIBrush::CreateObject
  375. //----------------------------------------------------------------------------------------
  376.  
  377. HGDIOBJ FW_CPrivGDIBrush::CreateObject()
  378. {
  379.     return fSolidColorBrush ? ::CreateSolidBrush(fColor) : fPattern->WinCreatePatternBrush();
  380. }
  381.  
  382. //----------------------------------------------------------------------------------------
  383. //    FW_CPrivGDIBrush::GetBrush
  384. //----------------------------------------------------------------------------------------
  385. //    Called when framing with a brush
  386.  
  387. HBRUSH FW_CPrivGDIBrush::GetBrush(HDC hdc)
  388. {
  389.     return (HBRUSH) GetObject(hdc);
  390. }
  391.  
  392. //========================================================================================
  393. //    class FW_CPrivGDIFont
  394. //========================================================================================
  395.  
  396. //----------------------------------------------------------------------------------------
  397. //    FW_CPrivGDIFont::FW_CPrivGDIFont
  398. //----------------------------------------------------------------------------------------
  399.  
  400. FW_CPrivGDIFont::FW_CPrivGDIFont() :
  401.     FW_CPrivGDIObject()
  402. {
  403. }
  404.  
  405. //----------------------------------------------------------------------------------------
  406. //    FW_CPrivGDIFont::~FW_CPrivGDIFont
  407. //----------------------------------------------------------------------------------------
  408.  
  409. FW_CPrivGDIFont::~FW_CPrivGDIFont()
  410. {
  411. }
  412.  
  413. //----------------------------------------------------------------------------------------
  414. //    FW_CPrivGDIFont::SetFontName
  415. //----------------------------------------------------------------------------------------
  416.  
  417. void FW_CPrivGDIFont::SetFontName(const char* fontName)
  418. {
  419.     if (fStockObject || fGDIObject == NULL || !FW_PrimitiveStringEqual(fFontName, fontName))
  420.     {
  421.         fStockObject = FALSE;
  422.         FW_PrimitiveStringCopy(fontName, fFontName);
  423.         fChanged = TRUE;
  424.     }
  425. }
  426.  
  427. //----------------------------------------------------------------------------------------
  428. //    FW_CPrivGDIFont::SetFontStyle
  429. //----------------------------------------------------------------------------------------
  430.  
  431. void FW_CPrivGDIFont::SetFontStyle(FW_FontStyle fontStyle)
  432. {
  433.     if (fontStyle != fFontStyle || fStockObject || fGDIObject == NULL)
  434.     {
  435.         fStockObject = FALSE;
  436.         fFontStyle = fontStyle;
  437.         fChanged = TRUE;
  438.     }
  439. }
  440.  
  441. //----------------------------------------------------------------------------------------
  442. //    FW_CPrivGDIFont::SetFontSize
  443. //----------------------------------------------------------------------------------------
  444.  
  445. void FW_CPrivGDIFont::SetFontSize(short fontSize)
  446. {
  447.     if (fontSize != fFontSize || fStockObject || fGDIObject == NULL)
  448.     {
  449.         fStockObject = FALSE;
  450.         fFontSize = fontSize;
  451.         fChanged = TRUE;
  452.     }
  453. }
  454.  
  455. //----------------------------------------------------------------------------------------
  456. //    FW_CPrivGDIFont::CreateObject
  457. //----------------------------------------------------------------------------------------
  458.  
  459. HGDIOBJ FW_CPrivGDIFont::CreateObject()
  460. {
  461.     LOGFONT logFont;
  462.     FW_CMemoryManager::SetMemory(&logFont, sizeof(logFont), 0x00);
  463.     
  464.     HDC screenDC = ::GetDC(NULL);
  465.     logFont.lfHeight = -MulDiv(fFontSize, ::GetDeviceCaps(screenDC, LOGPIXELSY), 72);
  466.     ::ReleaseDC(NULL, screenDC);
  467.     
  468.     FW_PrimitiveStringCopy(fFontName, logFont.lfFaceName); 
  469.  
  470.     if (fFontStyle & FW_kItalic)
  471.         logFont.lfItalic = 0xFF;
  472.     if (fFontStyle & FW_kUnderline)
  473.         logFont.lfUnderline = 0xFF;
  474.     if (fFontStyle & FW_kStrikeOut)
  475.         logFont.lfStrikeOut = 0xFF;
  476.     if (fFontStyle & FW_kBold)
  477.         logFont.lfWeight = FW_BOLD;
  478.     
  479.     return ::CreateFontIndirect(&logFont);
  480. }
  481.  
  482.  
  483.  
  484. #endif
  485.